//
//  PhotoViewController.m
//  Travel_M_Travel
//
//  Created by Sun on 15/4/5.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "PhotoViewController.h"
#import "RecommendHandler.h"
#import "AboutPhotoData.h"
#import "PhotoCViewCell.h"
#import "PHViewController.h"
#import "UIColor+AddColor.h"

@interface PhotoViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (nonatomic, retain) NSMutableArray *photoArr;
@property (nonatomic, retain) UICollectionView *collectView;

@end

@implementation PhotoViewController

- (void)dealloc
{
    [_photoArr release];
    [_collectView release];
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self dataHandler];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    
    
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 15, 0, 15);
    
    flowLayout.minimumLineSpacing = 10;
    
    self.collectView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 115)collectionViewLayout:flowLayout];
    _collectView.dataSource = self;
    _collectView.delegate = self;
    _collectView.backgroundColor = [UIColor colorFromHexCode:@"#f6f5ec"];
    
    // 注册cell
    [_collectView registerClass:[PhotoCViewCell class] forCellWithReuseIdentifier:@"phocell"];
    [self.view addSubview:_collectView];
    
    [flowLayout release];
    [_collectView release];

    
}

- (void)dataHandler
{
    NSString *urlStr = [[NSString stringWithFormat:@"http://api.breadtrip.com/featured/photos/?start=0"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [RecommendHandler getDataWithUrl:urlStr completion:^(NSDictionary *responseObject) {
        
        self.photoArr = [NSMutableArray array];
        NSMutableArray *tripArr = [responseObject objectForKey:@"items"];
        for (NSMutableDictionary *dic in tripArr) {
            AboutPhotoData *photoData = [[AboutPhotoData alloc] initWhihDictionary:dic];
            
            [self.photoArr addObject:photoData];
            [photoData release];
        }
        [_collectView reloadData];
    }];
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    
    return CGSizeMake(self.view.bounds.size.width, 10);
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.photoArr.count;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    PhotoCViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"phocell" forIndexPath:indexPath];
    AboutPhotoData *phoData = _photoArr[indexPath.item];
    cell.aboutPho = phoData;
    
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(140, 140);
    
}

- (void) collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    PHViewController *hpVC = [[PHViewController alloc] init];
    hpVC.photoData = self.photoArr[indexPath.item];
    [self.navigationController pushViewController:hpVC animated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
